package com.lzm.ydpt.utils;

import android.text.TextUtils;

import com.byt.framlib.commonutils.SharePerUtils;
import com.byt.framlib.commonutils.rx.RxBus;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.Dapplication;
import com.lzm.ydpt.entity.RoomDb;

import org.litepal.LitePal;

import java.util.List;

import me.leolin.shortcutbadger.ShortcutBadger;

/**
 * messagecount的工具类
 */
public class MessageCountUtil {
    private static MessageCountUtil instance;

    public static MessageCountUtil getInstance() {
        if (null == instance) {
            synchronized (MessageCountUtil.class) {
                if (null == instance) {
                    instance = new MessageCountUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 保存
     * 1 购物车
     * 增改 1是直接加  0 是修改
     */
    public int put(int messageType, String type_id) {
        RoomDb tempLectureRoomDb = getMessageData(String.valueOf(Dapplication.getUserInfo().getMemberId()), messageType, type_id);
        if (tempLectureRoomDb == null) {
            tempLectureRoomDb = new RoomDb(messageType, 1, String.valueOf(Dapplication.getUserInfo().getMemberId()), type_id);
            tempLectureRoomDb.save();
            return 1;
        } else {
            int count = tempLectureRoomDb.getMessageCount();
            tempLectureRoomDb.setMessageCount(count + 1);
            tempLectureRoomDb.save();
            return 0;
        }
    }

    /**
     * 保存
     * 1 购物车 5 咨询 12 系统 13 通知 27 微课 28.小安咨询
     * 增改 1是直接加  0 是修改
     */
    public int put(int messageType, String consumerId, int typeCount) {
        RoomDb tempLectureRoomDb = getMessageData(consumerId, messageType, "");
        if (tempLectureRoomDb == null) {
            tempLectureRoomDb = new RoomDb(messageType, typeCount, consumerId, "");
            tempLectureRoomDb.save();
            return 1;
        } else {
            tempLectureRoomDb.setMessageCount(typeCount);
            tempLectureRoomDb.save();
            return 0;
        }
    }


    /**
     * 删除并更新
     *
     * @param messageType
     * @param consumer_id
     * @param consumer_id
     * @param type_id
     * @param t
     */
    public <T> void removeAll(int messageType, String consumer_id, String type_id, T t) {
        int count = get(messageType, consumer_id, type_id);
        int unreadCount = SharePerUtils.getSharedInt(Constants.APP_UNREADCOUNT);
        if (unreadCount > count) {
            SharePerUtils.setSharedInt(Constants.APP_UNREADCOUNT, unreadCount - count);
            ShortcutBadger.applyCount(Dapplication.getAppContext(), unreadCount - count);
        } else {
            SharePerUtils.setSharedInt(Constants.APP_UNREADCOUNT, 0);
            ShortcutBadger.applyCount(Dapplication.getAppContext(), 0);
        }
        if (!TextUtils.isEmpty(type_id)) {
            LitePal.deleteAll(RoomDb.class, "consumer_id = ? and messageType = ? and type_id = ?", consumer_id, messageType + "", type_id);
        } else {
            LitePal.deleteAll(RoomDb.class, "consumer_id = ? and messageType = ? ", consumer_id, messageType + "");
        }
        if (t != null) {
            RxBus.getDefault().postSticky(t);
        }
    }

    /**
     * 查
     *
     * @param messageType
     * @param consumer_id
     * @param type_id
     * @return
     */
    public int get(int messageType, String consumer_id, String type_id) {
        int count = 0;
        if (!TextUtils.isEmpty(type_id)) { //不为空
            List<RoomDb> nullCounts = LitePal.where("consumer_id = ? and messageType = ? and type_id = ?", consumer_id, messageType + "", type_id).find(RoomDb.class);
            if (nullCounts != null && nullCounts.size() > 0) {
                int size = nullCounts.size();
                for (int i = 0; i < size; i++) {
                    count += nullCounts.get(i).getMessageCount();
                }
            }
        } else {
            List<RoomDb> dataCounts = LitePal.where("consumer_id = ? and messageType = ? ", consumer_id, messageType + "").find(RoomDb.class);
            if (dataCounts != null && dataCounts.size() > 0) {
                int size = dataCounts.size();
                for (int i = 0; i < size; i++) {
                    count += dataCounts.get(i).getMessageCount();
                }
            }
        }
        return count;
    }

    /**
     * 查找
     *
     * @param consumer_id
     * @param messageType
     * @param type_id
     * @return
     */
    private RoomDb getMessageData(String consumer_id, int messageType, String type_id) {
        if (!TextUtils.isEmpty(type_id)) { //不为空
            List<RoomDb> nullCounts = LitePal.where("consumer_id = ? and messageType = ? and type_id = ?", consumer_id, messageType + "", type_id).find(RoomDb.class);
            if (nullCounts != null && nullCounts.size() > 0) {
                int size = nullCounts.size();
                for (int i = 0; i < size; i++) {

                }
                return nullCounts.get(0);
            }
        } else {
            List<RoomDb> dataCounts = LitePal.where("consumer_id = ? and messageType = ? ", consumer_id, messageType + "").find(RoomDb.class);
            if (dataCounts != null && dataCounts.size() > 0) {
                return dataCounts.get(0);
            }
        }
        return null;
    }
}
